﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ecommerce2011.Models;
using ecommerce2011.BusinessInterface;
using ecommerce2011.Common;

namespace ecommerce2011.Controllers
{ 
    public class ProductController : Controller
    {
        private ProductosEcommerceEntities db = new ProductosEcommerceEntities();
        public const int PageSize = 10;
        private string CadenaBusqueda;
        private string CriterioBusqueda;

        //
        // GET: /Product/

        public ViewResult Index(int? page, string sort, string txtCadenaBusqueda, string selCriterioBusqueda)
        {
            //var product = db.Product.Where(p => p.ProductId > 0 && p.ProductId <= 100)
            //    .Include(p => p.City).Include(p => p.TransportationRate).Include(p => p.ShowRate)
            //    .Include(p => p.AccommodationRate)
            //    ;
            //return View(product.ToList());
            var sys = new GridSystem();
            var startPage = 0;
            if (page.HasValue && page.Value > 0) {
                startPage = page.Value - 1;
            }
            CadenaBusqueda = Session["CadenaBusqueda"] as string;
            if (CadenaBusqueda == null || txtCadenaBusqueda != null)
            {
                Session["CadenaBusqueda"] = txtCadenaBusqueda;
                CadenaBusqueda = txtCadenaBusqueda;
            }

            CriterioBusqueda = Session["CriterioBusqueda"] as string;
            if (CriterioBusqueda == null || selCriterioBusqueda != null)
            {
                Session["CriterioBusqueda"] = selCriterioBusqueda;
                CriterioBusqueda = selCriterioBusqueda;
            }
            GridProductViewModel list = sys.GetGridProductViewModel(startPage, PageSize, sort, CadenaBusqueda, CriterioBusqueda);
            return View(list);

        }

        //
        // GET: /Product/Details/5

        public ViewResult Details(long id)
        {
            Product product = db.Product.Find(id);
            Decimal total = 0;
            if (product.ShowRate != null)
                total += product.ShowRate.Price;
            if (product.City.CityRate != null)
                total += product.City.CityRate.Price;
            if (product.TransportationRate != null)
                total += product.TransportationRate.Price;
            if (product.AccommodationRate != null)
                total += product.AccommodationRate.Price;
            ViewData["Total"] = total;
            //Dummy
            //OmsServicesService.OmsServicesService cincoMejores = new OmsServicesService.OmsServicesService();
            //long[] cinco = cincoMejores.topFiveDummy(1);
            //Patricia
            //ProductosService.wsProductosService cincoMejores = new ProductosService.wsProductosService();
            //long[] cinco = cincoMejores.Top5ProductosMax(id);
            //OSB
            ProductosServiceProxy.wsProductosService cincoMejores = new ProductosServiceProxy.wsProductosService();
            long[] cinco = cincoMejores.Top5ProductosMax(id);

            List<Product> topCinco = new List<Product>();
            if (cinco != null)
            {
                foreach (long item in cinco)
                {
                    topCinco.Add(db.Product.Find(item));
                }
            }
            ViewData["topCinco"] = topCinco;

            return View(product);
        }

        //
        // GET: /Product/Create

        public ActionResult Create()
        {
            ViewBag.CityId = new SelectList(db.City, "CityId", "Name");
            ViewBag.TransportationRateId = new SelectList(db.Tarifa_Transporte, "TransportationRateId", "NameRate");
            ViewBag.ShowRateId = new SelectList(db.Tarifa_Show, "ShowRateId", "NameRate");
            ViewBag.AccommodationRateId = new SelectList(db.AccommodationRate, "AccommodationRateId", "NameRate");
            return View();
        } 

        //
        // POST: /Product/Create

        [HttpPost]
        public ActionResult Create(Product product)
        {
            if (ModelState.IsValid)
            {
                db.Product.Add(product);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.CityId = new SelectList(db.City, "CityId", "Name", product.CityId);
            ViewBag.TransportationRateId = new SelectList(db.Tarifa_Transporte, "TransportationRateId", "NameRate", product.TransportationRateId);
            ViewBag.ShowRateId = new SelectList(db.Tarifa_Show, "ShowRateId", "NameRate", product.ShowRateId);
            ViewBag.AccommodationRateId = new SelectList(db.AccommodationRate, "AccommodationRateId", "NameRate", product.AccommodationRateId);
            return View(product);
        }

        // POST: /Product/AddCart

        [HttpPost]
        public ActionResult AddCart(Int64 productId, int quantity)
        {
            List<CartItem> carrito = (List<CartItem>)Session["carrito"];
            Product product = db.Product.Find(productId);
            Decimal total = 0;
            if (carrito == null)
                carrito = new List<CartItem>();
            if (carrito.Exists(c => c.ProductId == productId))
            {
                carrito.Find(c => c.ProductId == productId).Quantity += quantity;
                if (product.ShowRate != null)
                    total += product.ShowRate.Price;
                if (product.City.CityRate != null)
                    total += product.City.CityRate.Price;
                if (product.TransportationRate != null)
                    total += product.TransportationRate.Price;
                if (product.AccommodationRate != null)
                    total += product.AccommodationRate.Price;
                carrito.Find(c => c.ProductId == productId).Name = product.Name;
                carrito.Find(c => c.ProductId == productId).UnitPrice = total;
                carrito.Find(c => c.ProductId == productId).TotalPrice = total * (carrito.Find(c => c.ProductId == productId).Quantity);
            }
            else
            {
                CartItem nuevo = new CartItem();
                nuevo.ProductId = productId;
                nuevo.Quantity = quantity;
                if (product.ShowRate != null)
                    total += product.ShowRate.Price;
                if (product.City.CityRate != null)
                    total += product.City.CityRate.Price;
                if (product.TransportationRate != null)
                    total += product.TransportationRate.Price;
                if (product.AccommodationRate != null)
                    total += product.AccommodationRate.Price;
                nuevo.Name = product.Name;
                nuevo.UnitPrice = total;
                nuevo.TotalPrice = total * quantity;
                carrito.Add(nuevo);
            }
            Session["carrito"] = carrito;
            return RedirectToAction("Details/"+productId.ToString());
        }
        
        //
        // GET: /Product/Edit/5
 
        public ActionResult Edit(long id)
        {
            Product product = db.Product.Find(id);
            ViewBag.CityId = new SelectList(db.City, "CityId", "Name", product.CityId);
            ViewBag.TransportationRateId = new SelectList(db.Tarifa_Transporte, "TransportationRateId", "NameRate", product.TransportationRateId);
            ViewBag.ShowRateId = new SelectList(db.Tarifa_Show, "ShowRateId", "NameRate", product.ShowRateId);
            ViewBag.AccommodationRateId = new SelectList(db.AccommodationRate, "AccommodationRateId", "NameRate", product.AccommodationRateId);
            return View(product);
        }

        //
        // POST: /Product/Edit/5

        [HttpPost]
        public ActionResult Edit(Product product)
        {
            if (ModelState.IsValid)
            {
                db.Entry(product).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.CityId = new SelectList(db.City, "CityId", "Name", product.CityId);
            ViewBag.TransportationRateId = new SelectList(db.Tarifa_Transporte, "TransportationRateId", "NameRate", product.TransportationRateId);
            ViewBag.ShowRateId = new SelectList(db.Tarifa_Show, "ShowRateId", "NameRate", product.ShowRateId);
            ViewBag.AccommodationRateId = new SelectList(db.AccommodationRate, "AccommodationRateId", "NameRate", product.AccommodationRateId);
            return View(product);
        }

        //
        // GET: /Product/Delete/5
 
        public ActionResult Delete(long id)
        {
            Product product = db.Product.Find(id);
            return View(product);
        }

        //
        // POST: /Product/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(long id)
        {            
            Product product = db.Product.Find(id);
            db.Product.Remove(product);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}